/**
 * 
 */
package com.cap.cfae;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.cap.cfae.cfae.UnderwriteAnaly;
import com.eos.das.entity.criteria.CriteriaType;
import com.eos.das.entity.criteria.ExprType;
import com.eos.das.entity.criteria.ExprType.OP;
import com.eos.foundation.database.DatabaseUtil;
import com.eos.system.annotation.Bizlet;

import commonj.sdo.DataObject;

/**
 * @author zp
 * @date 2018-01-28 13:43:29
 *
 */
@Bizlet("")
public class UnderwriteAnalyService {
	
	@Bizlet("getUnderwriteAnalyChart")
	public Map<String,Object> getUnderwriteAnalyChart(String year){
//		 DataObject  underwriteAnaly = DataObjectUtil.createDataObject("com.cap.cfae.cfae.UnderwriteAnaly");
//		 underwriteAnaly.set("issueUser", "安徽国贸集团控股有限公司");
		 CriteriaType criteria=CriteriaType.FACTORY.create();
		 criteria.set_entity(UnderwriteAnaly.QNAME);
		 List<ExprType> exprList=new ArrayList<ExprType>();
		 ExprType exprYear=ExprType.FACTORY.create();
		 exprYear.set_opEnum(OP.LIKE);
		 exprYear.set_property("issueDate");
		 exprYear.set_value(year);
		 ExprType exprName=ExprType.FACTORY.create();
		 exprName.set_opEnum(OP.EQ);
		 exprName.set_property("issueUser");
		 exprName.set_value("安徽国贸集团控股有限公司");
		 exprList.add(exprYear);
		 exprList.add(exprName);
		 criteria.set_expr(exprList);
		 DataObject[] underwriteAnalys= DatabaseUtil.queryEntitiesByCriteriaEntity("default", criteria)	;			
		 List<Map<String,String>> result=new ArrayList<Map<String, String>>();
		 Set<String> underwriteNameSet=new LinkedHashSet<String>();
		 for(int i=0;i<underwriteAnalys.length;i++)
		 {
			 underwriteNameSet.add(underwriteAnalys[i].getString("underwriteName"));
		 }
		 
		 for(String name:underwriteNameSet)
		 {
			 Map<String,String> map=new HashMap<String, String>();
			 
			 map.put("underwriteName", name);			 
			 map.put("N_1", "0");
			 map.put("N_2", "0");
			 map.put("N_3", "0");
			 map.put("N_4", "0");
			 map.put("N_5", "0");
			 map.put("N_6", "0");
			 
			 map.put("M_1", "0");
			 map.put("M_2", "0");
			 map.put("M_3", "0");
			 map.put("M_4", "0");
			 map.put("M_5", "0");
			 map.put("M_6", "0");
			 for(int j=0;j<underwriteAnalys.length;j++)
			 {								
				 if(name.equals(underwriteAnalys[j].getString("underwriteName")))
				 {
					 String issueType=underwriteAnalys[j].getString("bondType");
					 if(underwriteAnalys[j].getString("underwriteNum")!=null)
					 {
				      System.out.print(issueType);
				     map.put("N_"+issueType, String.valueOf(Integer.parseInt(map.get("N_"+issueType))+underwriteAnalys[j].getInt("underwriteNum")));
					 }
					 if(underwriteAnalys[j].getString("underwriteMoney")!=null)
					 {
				     map.put("M_"+issueType, String.valueOf(Double.parseDouble(map.get("M_"+issueType))+underwriteAnalys[j].getDouble("underwriteMoney")));
					 }
				 }
			 }
			 map.put("N_total", "0");
			 map.put("N_percent", "0");
			 map.put("M_total", "0");
			 map.put("M_percent", "0");
			 result.add(map);
		 }
         
		 Map<String ,Object[]> echartData=new HashMap<String, Object[]>();
		 if(result!=null&&result.size()>0)
		 {
			 int size=result.size();
		      String [] nameArray=new String[size];
		      Integer[] n1Array=new Integer[size];
		      Integer[] n2Array=new Integer[size];
		      Integer[] n3Array=new Integer[size];
		      Integer[] n4Array=new Integer[size];
		      Integer[] n5Array=new Integer[size];
		      Integer[] n6Array=new Integer[size];
		      for(int i=0;i<result.size();i++)
		      {
			      Map<String,String> map=result.get(i);
			      nameArray[i]=map.get("underwriteName");
			      n1Array[i]=Integer.parseInt(map.get("N_1"));
			      n2Array[i]=Integer.parseInt(map.get("N_2"));
			      n3Array[i]=Integer.parseInt(map.get("N_3"));
			      n4Array[i]=Integer.parseInt(map.get("N_4"));
			      n5Array[i]=Integer.parseInt(map.get("N_5"));
			      n6Array[i]=Integer.parseInt(map.get("N_6"));
		      }
		      echartData.put("CXS",nameArray);
		      echartData.put("N_1",n1Array);
		      echartData.put("N_2",n2Array);
		      echartData.put("N_3",n3Array);
		      echartData.put("N_4",n4Array);
		      echartData.put("N_5",n5Array);
		      echartData.put("N_6",n6Array);
		 }
		 Map<String,Object> data=new HashMap<String, Object>();
		 data.put("echartData", echartData);
		 data.put("tableData", result);
		return data;
	}

}
